<template>
	<div class="" style="width: 100%;">
		<head-top></head-top>


	
		<div class="table_container" :style="'width: 90%;  overflow: hidden;  height:'+(winHeight/1.2)+'px;'">
			<div style="margin: 5px; display: flex;">
				<span style="font-size: 20px;width: 100px;">申赎表 :  </span>
				<div>
					<el-button size="mini" type="success" @click="refleash()">刷新</el-button>
					<el-button size="mini" type="primary" @click="addnew_arrange()">新增</el-button>
				</div>

				<span style="font-size: medium; padding: 10px;">账号:</span>
				<el-select v-model="account_choice" size="small" clearable filterable placeholder="请选择"
					@change="changeaccount_choice()">
					<el-option v-for="item in account_options" :key="item.label" :label="item.label"
						:value="item.value">
					</el-option>
				</el-select>

				<span style="font-size: medium; padding: 10px;">日期:</span>
				<el-select v-model="date_choice" size="small" clearable filterable placeholder="请选择"
					@change="changedate_choice()">
					<el-option v-for="item in date_options" :key="item.label" :label="item.label" :value="item.value">
					</el-option>
				</el-select>


				
			</div>

			<el-table ref="singleTable" :data="arrange_data" height="100%" border size='small' :fit='true'
				:style="'width:'+(screenWidth*1.0)+'px;'" :row-class-name=" tableRowClassNamemoneyout">
				<el-table-column prop="index" label="id" width="50" align></el-table-column>
				
				<el-table-column prop="trade_type" label="操作类型" width="100"   align>
					<template slot-scope="scope">
						<el-select v-show="scope.row['status'].indexOf('完成') == -1" size="small" v-model="scope.row['trade_type']"
							
							style="" placeholder="请选择"
							@change=" change_cell($event,scope)">
							<el-option v-for="item in tradeside_options" :key="item.value" :label="item.label"
								:value="item.value">
							</el-option>
						</el-select>
					    <span  v-show="scope.row['status'].indexOf('完成') != -1" > {{scope.row['tradeside'] }}</span> 
					</template>
				</el-table-column>
	
				<el-table-column prop="account" label="账户" width="200"   align>
					<template slot-scope="scope">
						<el-select  v-show="scope.row['status'].indexOf('完成') == -1" v-model="scope.row['account']" size="small" allow-create clearable filterable
							 style="" placeholder="请选择"
							@change=" change_cell($event,scope)">
							<el-option v-for="item in account_options" :key="item.value" :label="item.label"
								:value="item.value">
							</el-option>
						</el-select>
						<span  v-show="scope.row['status'].indexOf('完成') != -1" > {{scope.row['account'] }}</span> 
					</template>
				</el-table-column>
				<!-- <el-table-column prop="account_zw" label="账户中文" width="100"   align>
					<template slot-scope="scope">
						<el-input  v-show="scope.row['status'].indexOf('完成') == -1" v-model="scope.row['account_zw']" 
							size="small" placeholder="" @change=" change_cell($event,scope)"></el-input>
						<span  v-show="scope.row['status'].indexOf('完成') != -1" > {{scope.row['account_zw'] }}</span> 
					</template>
					
				</el-table-column> -->

				
				<!-- <el-table-column prop="account_code" label="账号" width="220"   align>
					<template slot-scope="scope">
						<el-input  v-show="scope.row['status'].indexOf('完成') == -1" v-model="scope.row['account_code']" 
							size="small" placeholder="" @change=" change_cell($event,scope)"></el-input>
						<span  v-show="scope.row['status'].indexOf('完成') != -1" > {{scope.row['account_code'] }}</span> 	
					</template>
					
				</el-table-column> -->
				
				
				<el-table-column prop="kkdate" label="截至日期" width="120"   align>
					<template slot-scope="scope">
						<el-input  v-show="scope.row['status'].indexOf('完成') == -1"  v-model="scope.row['kkdate']" 
							size="small" placeholder="" @change=" change_cell($event,scope)"></el-input>
						<span  v-show="scope.row['status'].indexOf('完成') != -1" > {{scope.row['kkdate'] }}</span> 
					</template>
					
				</el-table-column>
				
				
			
			
				<el-table-column prop="time" label="截至时间" width="100"   align>
					<template slot-scope="scope">
						<el-select  v-show="scope.row['status'].indexOf('完成') == -1" v-model="scope.row['time']" size="small" clearable filterable allow-create
							collapse-tags style="" placeholder="请选择"
							@change=" change_cell($event,scope)">
							<el-option v-for="item in time_list" :key="item.value" :label="item.label"
								:value="item.value">
							</el-option>
						</el-select>
						<span  v-show="scope.row['status'].indexOf('完成') != -1" > {{scope.row['time'] }}</span> 
					</template>
					
				</el-table-column>
			
			
			
				<el-table-column prop="asker" label="需求人" width="120"   align>
					<template slot-scope="scope">
						<el-select  v-show="scope.row['status'].indexOf('完成') == -1" v-model="scope.row['asker']" size="small" clearable filterable allow-create
							collapse-tags style="" placeholder="请选择"
							@change=" change_cell($event,scope)">
							<el-option v-for="item in worker_list" :key="item.value" :label="item.label"
								:value="item.value">
							</el-option>
						</el-select>
						<span  v-show="scope.row['status'].indexOf('完成') != -1" > {{scope.row['asker'] }}</span> 
					</template>
					
				</el-table-column>
						
			
				<el-table-column prop="buy_money" label="申购金额(万)" width="90" align>
					<template slot-scope="scope">
						<el-input  v-show="scope.row['status'].indexOf('完成') == -1"  v-model="scope.row['buy_money']"
							size="small" placeholder="" @change=" change_cell($event,scope)"></el-input>
						<span  v-show="scope.row['status'].indexOf('完成') != -1" > {{scope.row['buy_money'] }}</span> 
					</template>
				</el-table-column>
				
				
				<el-table-column prop="sell_share" label="赎回份额" width="90" align>
					<template slot-scope="scope">
						<el-input  v-show="scope.row['status'].indexOf('完成') == -1"  v-model="scope.row['sell_share']"
							size="small" placeholder="" @change=" change_cell($event,scope)"></el-input>
						<span  v-show="scope.row['status'].indexOf('完成') != -1" > {{scope.row['sell_share'] }}</span> 
					</template>
				</el-table-column>
				
				
				<el-table-column prop="log" label="备注" align>
					<template slot-scope="scope">
						<el-input  v-show="scope.row['status'].indexOf('完成') == -1" v-model="scope.row['log']" 
							size="small" placeholder="" @change=" change_cell($event,scope)"></el-input>
						<span  v-show="scope.row['status'].indexOf('完成') != -1" > {{scope.row['log'] }}</span> 
					</template>
				</el-table-column>
				
				
				<el-table-column prop="sender" label="抄送人" width="120"   align>
					<template slot-scope="scope">
						<el-select  v-show="scope.row['status'].indexOf('完成') == -1" v-model="scope.row[sender]" size="small" clearable filterable allow-create
							collapse-tags style="" placeholder="请选择"
							@change=" change_cell($event,scope)">
							<el-option v-for="item in worker_list" :key="item.value" :label="item.label"
								:value="item.value">
							</el-option>
						</el-select>
						<span  v-show="scope.row['status'].indexOf('完成') != -1" > {{scope.row['sender'] }}</span> 
					</template>
				</el-table-column>

				<!-- <el-table-column prop="kkupdatetime" label="更新时间" width="140" align></el-table-column> -->
				<el-table-column prop="status" label="状态" width="120" align>
					<template slot-scope="scope">
						<!-- :disabled="scope.row['status']=='完成' " -->
						<el-select  v-model="scope.row['status']" size="small" collapse-tags style="" placeholder="请选择"
							@change=" change_cell($event,scope)">
							<el-option v-for="item in status_options" :key="item.value" :label="item.label"
								:value="item.value">
							</el-option>
						</el-select>
					</template>
				</el-table-column>
				
				<el-table-column label="操作" :width='90'>
					<template slot-scope="scope">
						<el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除
						</el-button>
						<!-- <el-button size="small" type="warning" @click="index(scope.$index, scope.row)">详情</el-button> -->
					</template>
				</el-table-column>
			</el-table>

		</div>
	
	</div>
</template>

<script>
	import headTop from '@/components/headTop'
	export default {
		data() {
			return {
				checkflag: false,
				core_pos: [],
				pos_index_item: '',
				account_choice: "",
				account_options: [],
				target_pos_index_options: [],
				date_choice: "",
				date_options: [],
				tableData: [],
				currentRow: null,
				
				
				count: 0,
				exchange_detail: [],
				LP_account_list: [],
			
				fix_account: {},
				visible: false,
				visible2: false,
			

				screenWidth: document.body.clientWidth, // 屏幕尺寸
				screenHeight: document.body.clientHeight, // 屏幕尺寸
				buyindex: "0",
				arrange_data: [],

			
				
				time_list:[
				{
					value: '8:40',
					label: '8:40'
				},
				
				
				{
					value: '12:30',
					label: '12:30'
				},
				
				{
					value: '15:30',
					label: '15:30'
				},
				
				
				],	
			
				status_options: [{
						value: '待处理',
						label: '待处理'
					},

					{
						value: '异常',
						label: '异常'
					},
					{
						value: '完成',
						label: '完成'
					},
					{
						value: '准备',
						label: '准备'
					},

				],
				direction_options: [{
						value: '银行转证券',
						label: '银行转证券'
					},
					{
						value: '银行转期货',
						label: '银行转期货'
					},
					
					{
						value: '证券转银行',
						label: '证券转银行'
					},
					
					{
						value: '期货转银行',
						label: '期货转银行'
					},
					
					{
						value: '期货转证券',
						label: '期货转证券'
					},
					{
						value: '证券转期货',
						label: '证券转期货'
					},
				
				],
				tradeside_options: [
					{
						value: '申购',
						label: '申购'
					},
					{
						value: '赎回',
						label: '赎回'
					},
					{
						value: '资金到账',
						label: '资金到账'
					},

				],

			}
		},
		components: {
			headTop,
		},



		activated() {

			// console.log("当前登入用户", this.$store.state.adminInfo.name);
			//    if (this.tradesysPermission_list.indexOf (this.$store.state.adminInfo.permission) ==-1) {
			// 	console.log(this.$store.state.adminInfo.name,"未登入")
			// 	this.$message({type: 'error',	message: '没有权限',});
			// 	this.$router.push({
			// 		path: `/manage`
			// 	})
			// }
		},
		created() {
			this.get_tradesys_account()
			this.get_exchange_arrange()
			this.get_date_last()
		},
		computed: {
			
		},

		methods: {
			changedate_choice() {

				this.get_exchange_arrange()
			},

			changeaccount_choice() {
				this.get_exchange_arrange()
			},
			change_cell($event, scope) {
				// console.log(scope.$index)
				var that = this
				console.log(scope)
				var arrange_data_item= this.arrange_data[scope.$index]
				if (scope.column['label']=='账户'){
					for (var i = 0; i < that.LP_account_list.length; i++) {
						if( that.LP_account_list[i]['account']==this.arrange_data[scope.$index]['account']){
							this.arrange_data[scope.$index]['account_zw']=that.LP_account_list[i]['account_zwname']
						};
					}
				
				}
				
				
				
				let sender=arrange_data_item['sender']
				let msg= `申赎编号: ${arrange_data_item['index']} 操作: ${arrange_data_item['trade_type']} \n`     
				msg+= `账户: ${arrange_data_item['account']} ${arrange_data_item['account_zw']}\n` 
				msg+= `金额: ${arrange_data_item['buy_money']}万元  份额:${arrange_data_item['sell_share']}  \n`
				msg+= `截至时间: ${arrange_data_item['kkdate']} ${arrange_data_item['time']}   \n`
				msg+= `需求人: ${arrange_data_item['asker']} 执行人: ${arrange_data_item['sender']}  \n`
				msg+= `备注: ${arrange_data_item['log']}  \n`
				msg+= `工单状态: ${arrange_data_item['status']}  `
				
				
				
				let  user_open_id='ou_40fae1aeef8177cb25ed31858b847a91' //胡睦
				if (sender=='陈希瑞'){
					 user_open_id='ou_40fae1aeef8177cb25ed31858b847a91' //陈希瑞
					
				}
				
				if (arrange_data_item['status']=='待处理' && scope.column['label']=='状态'){
					that.$axios.post(that.lpapi2, "form=send_feishu_msg&user="+user_open_id+"&msg="+msg+"&chart_id=oc_60396b087b4d656c6e718b4c7feb8ae4").then(response => {
						console.log(response, 'send_msg');
						if (response.status = 200) {
							let a=1
							// this.get_exchange_arrange()
						} else {
							console.log(response.data.statusCode);
						}
						
					})
				}
				
				// this.$message({
				// 	message: '已经修改',
				// 	type: 'warning'
				// });

				


				let sql2 = "UPDATE `LP_tradesys_buy` SET \
					`account_zw` = '" + arrange_data_item['account_zw'] + "', `date` = '" + arrange_data_item['kkdate'] + "', \
					`account` = '" + arrange_data_item['account'] + "', `sell_share` = '" + arrange_data_item['sell_share'] + "',   \
					`buy_money` = '" + arrange_data_item['buy_money'] + "', `trade_type` = '" + arrange_data_item['trade_type'] + "',   \
					`time` = '" + arrange_data_item['time'] + "', \
					`asker` = '" + arrange_data_item['asker'] + "', `sender` = '" + arrange_data_item['sender'] + "',  \
					`log` = '" + arrange_data_item['log'] + "' ,`status` = '" + arrange_data_item['status'] + "'\
					WHERE `index` = " + arrange_data_item['index'] + ";"

				that.$axios.post(that.lpapi2, "form=do_sql&sql=" + sql2).then(response => {
					console.log(response, 'do_sql');
					if (response.status = 200) {
						this.$message({
							message: '更新',
							type: 'success'
						});
						// this.get_exchange_arrange()
					} else {
						return
						console.log(response.data.statusCode);
					}
				})




				// }).catch(() => {
				// 	that.$message({
				// 		type: 'info',
				// 		message: '已取消修改'
				// 	});
				// 	that.get_exchange_arrange()
				// });


			},
			
			

			
			handlefixaccount(index, row) {
				console.log("row", row)
				this.fix_account = row
				this.visible2 = true
			},
			handleDelete(index, row) {

				console.log("row", row['index'])
				this.buyindex = row['index']
				// this.exchange_arrange=row
				var that = this;

				this.$confirm('此操作将永久修改数据, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',inputType: 'password',
					type: 'warning'
				}).then(() => {


					let sql2 = "delete from `LP_tradesys_buy`  WHERE `index`='" + this.buyindex + "';"

					that.$axios.post(that.lpapi2, "form=do_sql&sql=" + sql2).then(response => {
						console.log(response, 'do_sql');
						if (response.status = 200) {
							this.$message({
								message: '删除完成',
								type: 'success'
							});
							this.get_exchange_arrange()
						} else {
							return
							console.log(response.data.statusCode);
						}
					})


				}).catch(() => {
					this.$message({
						type: 'info',
						message: '取消删除'
					});
				});
			},

		
			refleash() {
				this.get_exchange_arrange()
			},
			addnew_arrange() {
				var that = this;
				var date = new Date();
				var seperator1 = "-";
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				var strDate = date.getDate();
				if (month >= 1 && month <= 9) {
					month = "0" + month;
				}
				if (strDate >= 0 && strDate <= 9) {
					strDate = "0" + strDate;
				}
				
			
				
				
				
				var currentdate = year + seperator1 + month + seperator1 + strDate;
				let sql2 = "INSERT INTO `LP_tradesys_buy`(`date`,`status`,`updatetime`,`buy_money`,`sell_share`,`trade_type`,`asker`,`sender`) VALUES ('" +
					currentdate + "','准备',CURRENT_TIMESTAMP,'0','0','申购','安姣姣','胡睦');"
				that.$axios.post(that.lpapi2, "form=do_sql&sql=" + sql2).then(response => {
					console.log(response, 'do_sql');
					if (response.status = 200) {
						this.$message({
							message: '新增完成',
							type: 'success'
						});
						this.get_exchange_arrange()
					} else {
						return
						console.log(response.data.statusCode);
					}
				})

			},

			get_date_last() {
				var that = this;
				
				
				let sql =
					" select date_format(date,'%Y-%m-%d') as kkdate ,date from `LP_tradesys_buy`  GROUP BY date;"
				that.$axios.post(that.lpapi2, "form=get_data&sql=" + sql).then(response => {
					console.log(response, 'data_tradesys_account');
					if (response.status = 200) {
						that.date_options = []
						//for (var i = 0; i < response.data.length; i++) {
						for (var i = response.data.length - 1; i >= 0; i--) {
							let char = {
								value: response.data[i]['kkdate'],
								label: response.data[i]['kkdate'],
							};
							that.date_options.push(char);
						}
					} else {
						console.log(response.data.statusCode);
					}
				})

			},



			get_tradesys_account() {
				var that = this;
				// console.log(that.lpapi2)

				let sql =
					"select * from `LP_account_list` where check_flag ='yes' ORDER BY account_type  ;"
				that.$axios.post(that.lpapi2, "form=get_data&sql=" + sql).then(response => {

					console.log(response, 'conf_tradesys_account')
					let plat_list = []
					if (response.status = 200) {
						that.account_options = []
						that.LP_account_list = response.data;
						for (var i = 0; i < response.data.length; i++) {
							let char = {
								value: response.data[i]['account'],
								// label: response.data[i]['account'],
								label: response.data[i]['account']+'/'+response.data[i]['account_zwname'],

							};

							that.account_options.push(char);
						}

					} else {
						console.log(response.data.statusCode);
					}
				})


			},
			get_exchange_arrange() {
				var that = this;
				console.log(that.lpapi2)
				
				
				
				
				let sql =
					"select * ,date_format(date,'%Y-%m-%d') as kkdate,date_format(updatetime,'%Y-%m-%d  %H:%i:%s') as kkupdatetime \
					from `LP_tradesys_buy`  ORDER BY date desc,time  limit 100;"
			
				if (this.account_choice != "" && this.date_choice != "") {
					sql =
						"select * ,date_format(date,'%Y-%m-%d') as kkdate,date_format(updatetime,'%Y-%m-%d  %H:%i:%s') as kkupdatetime \
					from `LP_tradesys_buy` where  account='" + this.account_choice + "' and  date='" + this.date_choice +
						"' ORDER BY date desc,time  limit 100;"
				} else if (this.account_choice != "") {
					sql =
						"select * ,date_format(date,'%Y-%m-%d') as kkdate,date_format(updatetime,'%Y-%m-%d  %H:%i:%s') as kkupdatetime \
					from `LP_tradesys_buy` where  account='" + this.account_choice +
						"'  ORDER BY date desc,time  limit 100;"
				} else if (this.date_choice != "") {
					sql =
						"select * ,date_format(date,'%Y-%m-%d') as kkdate,date_format(updatetime,'%Y-%m-%d  %H:%i:%s') as kkupdatetime \
					from `LP_tradesys_buy` where   date='" + this.date_choice + "'  ORDER BY date desc,time  limit 100;"
				}
				
				that.arrange_data = []
				that.$axios.post(that.lpapi2, "form=get_data&sql=" + sql).then(response => {

					console.log(response.data[0], 'LP_tradesys_buy')

					if (response.status = 200) {
						if (response.data[0].hasOwnProperty('message')) {
							that.arrange_data = []
						} else {
							that.arrange_data = response.data;
						}
						// that.arrange_data = response.data;
					} else {
						console.log(response.data.statusCode);
					}
				})


			},




			
			tableRowClassNamemoneyout({
				row,
				rowIndex
			}) {
		
				if (this.arrange_data[rowIndex]['status'] == '待处理' | this.arrange_data[rowIndex]['status'] == '准备' ) {
					return 'bacorange';
				}
				if (this.arrange_data[rowIndex]['status'] == '异常') {
					return 'bacOrangeRed';
				}
				return '';
			}

		},
	}
</script>

<style>
	.el-table .bacOrangeRed {
		background: OrangeRed;
	}

	.el-table .bacorange {
		background: orange;
	}

	.el-table .success-row {
		background: grey;
		color: white;
	}

	.table_container {
		margin: 20px 5%;
		/* background-color: #0000FF; */
	}

	.el-table .grey {
		background: Silver;
	}

	.el-table .DarkGray {
		background: DarkGray;
	}

	.el-table .LightCoral {
		background: LightCoral;
		color: white;
	}

	.el-table .LimeGreen {
		background: LimeGreen;
	}

	.el-table .green {
		background: green;
	}

	.el-table .LightSlateGray {
		background: LightSlateGray;
	}

	.el-table .red {
		background: red;
		color: white;
	}

	.el-table .orange {
		background: NavajoWhite;
		color: black;
	}

	.el-table .blue {
		background: blue;
	}

	.el-table .purple {
		background: Violet;
		color: black;
	}

	.el-table .LightSteelBlue {
		background: LightSteelBlue;
	}

	.el-table .Goldenrod {
		background: Goldenrod;
	}

	.el-table .xingmu {
		background: Goldenrod;
		color: red;
		font-weight: bold;

	}


	.el-table .black {
		background: black;
		color: white;
	}

	.el-table .green {
		background: green;
		color: white;
	}

	.el-table .CornflowerBlue {
		background: CornflowerBlue;
		color: white;
	}

	.el-table .DodgerBlue {
		background: DodgerBlue;
	}
</style>
